﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLNhaThuoc.DTO;
using System.Data;
using System.Collections;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc.DAL
{
    class LoaiThuocDA:DataProvider
    {
        public LoaiThuocDA()
        {
            connect();
            fillMyTable("LOAI_THUOC");
        }

        public void insert(LoaiThuoc info)
        {
            DataRow row = myTable.NewRow();
            row["MaLoai"] = info.MaLoai;
            row["Ten"] = info.Ten;
            myTable.Rows.Add(row);

            updateMyTable();
        }

        public void delete(LoaiThuoc info)
        {
            myTable.Rows.Find(info.MaLoai).Delete();

            updateMyTable();
        }

        //public void update(LoaiThuoc old_info, LoaiThuoc info)
        //{
        //    LoaiThuocObserver observer = new LoaiThuocObserver();

        //    observer.update(old_info, info);
        //}

        public void update(LoaiThuoc info)
        {
            DataRow row = myTable.Rows.Find(info.MaLoai);
            row["Ten"] = info.Ten;

            updateMyTable();
        }

        public bool KiemTraPK(LoaiThuoc info)
        {
            DataRow row = myTable.Rows.Find(info.MaLoai);
            if (row != null)
                return true;
            return false;
        }

        public LoaiThuoc[] LayListLoaiThuoc()
        {
            ArrayList list = DongHopDuLieuTuDataTable();
            int n = list.Count;
            LoaiThuoc[] kq = new LoaiThuoc[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (LoaiThuoc)list[i];
            }

            return kq;
        }

        public override object ConvertOneRowIntoDTO(DataTable myTable, int i)
        {
            LoaiThuoc obj = new LoaiThuoc();
            obj.MaLoai = myTable.Rows[i]["MaLoai"].ToString();
            obj.Ten = myTable.Rows[i]["Ten"].ToString();

            return obj;
        }
    }
}
